import type { DictItemDetailResult, DictItemEntityReq, DictItemListResult, DictItemOptionResult } from "@/api/types";
import request from "@/utils/http";

/** 新增字典项 */
function create(data: DictItemEntityReq): Promise<Resp<string>> {
    return request({
        url: "/sys/dictItem/create",
        method: "post",
        data,
    });
}

/** 删除字典项 */
function remove(id: string): Promise<Resp<null>> {
    return request({
        url: "/sys/dictItem/remove",
        method: "post",
        data: { id },
    });
}

/** 修改字典项 */
function update(data: DictItemEntityReq): Promise<Resp<null>> {
    return request({
        url: "/sys/dictItem/update",
        method: "post",
        data,
    });
}

/** 设置数据字典项默认值 */
function updateDefault(id: string, typeCode: string): Promise<Resp<null>> {
    return request({
        url: "/sys/dictItem/updateDefault",
        method: "post",
        data: { id, typeCode },
    });
}

/** 取消设置数据字典项默认值 */
function cancelDefault(id: string): Promise<Resp<null>> {
    return request({
        url: "/sys/dictItem/cancelDefault",
        method: "post",
        data: { id },
    });
}

/** 查询单个记录 */
function findById(id: string): Promise<Resp<DictItemDetailResult>> {
    return request({
        url: "/sys/dictItem/findById",
        method: "get",
        params: { id },
    });
}

/** 获取下拉选项 */
function listOptions(typeCodes: string | string[]): Promise<Resp<Record<string, DictItemOptionResult[]>>> {
    if (!Array.isArray(typeCodes)) {
        typeCodes = [typeCodes];
    }
    return request({
        url: "/sys/dictItem/listOptions",
        method: "get",
        params: { typeCodes },
    });
}

/** 获取列表 */
function list(typeCode: string): Promise<Resp<DictItemListResult[]>> {
    return request({
        url: "/sys/dictItem/list",
        method: "get",
        params: { typeCode },
    });
}

export const dictItemApi = {
    create,
    remove,
    update,
    updateDefault,
    cancelDefault,
    findById,
    listOptions,
    list,
};
